using IQIGame.Rendering;
using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;

[ExecuteAlways]
//[IQIGameSign("溶解", GameRender.PropertyType.None, 3)]
public class DissolveShading : MonoBehaviour
{
    [BoxGroup("溶解", CenterLabel = true)]
    //[IQIGameSign("溶解贴图", GameRender.PropertyType.Texture)]
    [LabelText("噪声贴图替换")]
    public Texture Noise;

    [BoxGroup("溶解", CenterLabel = true)]
    //[IQIGameSign("溶解贴图ST", GameRender.PropertyType.Vector)]
    [LabelText("溶解贴图ST")]
    public Vector4 NoiseST = new Vector4(1, 1, 0, 0);

    [BoxGroup("溶解", CenterLabel = true)]
    [LabelText("溶解起始偏移")]
    public Vector3 Center = Vector3.zero;

    [BoxGroup("溶解", CenterLabel = true)]
    //[IQIGameSign("溶解系数", GameRender.PropertyType.Float)]
    [LabelText("溶解系数")]
    [Range(0, 1)]
    public float Factor = 0.0f;

    [BoxGroup("溶解", CenterLabel = true)]
    //[IQIGameSign("溶解范围", GameRender.PropertyType.Float)]
    [LabelText("溶解范围")]
    [Range(0, 1)]
    public float Range = 0.0f;

    [BoxGroup("溶解", CenterLabel = true)]
    [LabelText("溶解长度")]
    public float MaxLength = 0.0f;

    [BoxGroup("溶解", CenterLabel = true)]
    [LabelText("溶解网格拉伸")]
    public float Scale = 0.0f;

    [BoxGroup("溶解", CenterLabel = true)]
    //[IQIGameSign("溶解颜色强度", GameRender.PropertyType.Float)]
    [LabelText("溶解颜色强度")]
    public float intensity = 1;

    [BoxGroup("溶解", CenterLabel = true)]
    //[IQIGameSign("溶解颜色", GameRender.PropertyType.Color)]
    [LabelText("溶解颜色")]
    public Color color = Color.white;

    [BoxGroup("溶解", CenterLabel = true)]
    [LabelText("溶解颜色过渡")]
    [Range(0, 1)]
    public float Fade = 0.0f;


    private void Awake()
    {
        mats.Clear();
        renderer = null;
    }

    void Update()
    {
        if (!this.enabled || Materials == null || Materials.Count < 1) return;

        for (int i = 0; i < Materials.Count; i++)
        {
            if (Noise != null && Materials[i].HasProperty(GameRender.ShaderKeywords.NoiseTex))
            {
                Materials[i].SetTexture(GameRender.ShaderKeywords.NoiseTex, Noise);
                Materials[i].SetTextureScale(GameRender.ShaderKeywords.NoiseTex, new Vector2(NoiseST.x, NoiseST.y));
                Materials[i].SetTextureOffset(GameRender.ShaderKeywords.NoiseTex, new Vector2(NoiseST.z, NoiseST.w));
            }
            //溶解参数设置
            if (Materials[i].HasProperty(GameRender.ShaderKeywords.CharacterDissovleParam))
            {
                Materials[i].SetVector(GameRender.ShaderKeywords.CharacterDissovleParam, new Vector4(Factor, Fade, Range, Scale));
                Materials[i].SetVector(GameRender.ShaderKeywords.CharacterDissolveCenter, new Vector4(Center.x, Center.y, Center.z, MaxLength));

                var c = new Vector4(color.r, color.g, color.b, 1) * intensity;
                Materials[i].SetColor(GameRender.ShaderKeywords.CharacterDissovleColor, c);
            }
        }
    }

    #region materils

    new Renderer[] renderer;
    Renderer[] MeshRender
    {
        get
        {
            if (renderer == null)
                renderer = GetComponentsInChildren<Renderer>();
            return renderer;
        }
    }

    [SerializeField]
    [LabelText("所有子材质")]
    [Space(10)]
    List<Material> mats = new List<Material>();
    List<Material> Materials
    {
        get
        {
            if (mats == null)
                mats = new List<Material>();
            GameRender.TryFindMaterialsByRenderer(MeshRender, mats, GameRender.ToneShaderTitle);
            return mats;
        }
    }
    #endregion
}
